package com.tzxs.pay.main.login.model;

import android.app.Application;

import com.tzxs.pay.main.first.model.MsgNoticeBean;
import com.tzxs.pay.main.first.model.PendingOrderBean;
import com.tzxs.pay.main.mine.model.MessageBean;
import com.tzxs.pay.main.mine.model.PasswordBean;
import com.tzxs.pay.util.ISharedPreference;
import com.tzxs.pay.util.RetrofitUtil;
import com.tzxs.pay.util.RxUtil;
import com.tzxs.pay.util.netnavigator.NetNavigator;
import com.tzxs.pay.util.netnavigator.NetNavigatorWrapper;
import com.tzxs.pay.util.websocket.model.WebSocketBean;

import java.lang.ref.WeakReference;

import retrofit2.Response;


public class UserDataResource {
    private static final String TAG = UserDataResource.class.getSimpleName();
    private static UserDataResource instance = null;
    private WeakReference<Application> appWeak;

    /**
     * 采用弱引用的方式传入,其实没必要,因为application对象是肯定存在的
     *
     * @param appWeak application的弱引用对象
     */
    private UserDataResource(WeakReference<Application> appWeak) {
        this.appWeak = appWeak;
    }

    /**
     * 单例
     *
     * @param app
     * @return instance
     */
    public static UserDataResource getInstance(Application app) {
        if (instance == null) {
            synchronized (UserDataResource.class) {
                if (instance == null) {
                    instance = new UserDataResource(new WeakReference<>(app));
                }
            }
        }
        return instance;
    }

    //登录方法
    public void login(String phone, String password, final NetNavigator<LoginBean> netNavigator) {
        NetNavigator<LoginBean> netNavigatorWapper = new NetNavigatorWrapper<LoginBean>(netNavigator) {
            //我在这里重写success方法,就可以获得数据
            @Override
            public void success(LoginBean loginBean) {
                super.success(loginBean);
                //保存用户的token
                ISharedPreference.getInstance(appWeak.get())
                        .saveToken(loginBean.getToken());
                //保存用户手机号
                ISharedPreference.getInstance(appWeak.get())
                        .savePhone(phone);

            }
        };
        //然后传入代理对象就可以了
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.login(phone, password), netNavigatorWapper);
    }


    //获取验证码
    public void getVerCode(String phone, NetNavigator<MessageBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.getVerCode(phone), navigator);
    }


    //修改支付密码
    public void changePassword(String newPassword, String surePassword,
                               String phone, String code,
                               NetNavigator<PasswordBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.
                changePassword(ISharedPreference.getInstance(appWeak.get()).getToken(),
                        newPassword, surePassword, phone, code), navigator);
    }


    //获取通知列表
    public void getNoticeList(int offset, int limit, NetNavigator<MsgNoticeBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.noticeList(ISharedPreference.getInstance(appWeak.get()).getToken(), offset, limit), navigator);

    }


    //通知详情列表
    public void noticeDetail(int offset, int limit, NetNavigator<PendingOrderBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.noticeDetail(ISharedPreference.getInstance(appWeak.get()).getToken(), offset, limit), navigator);
    }
}
